package com.fitness.service.serviceimpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.fitness.mapper.ManagementMapper;
import com.fitness.model.Address;
import com.fitness.model.AdminUser;
import com.fitness.model.Camp;
import com.fitness.model.Class;
import com.fitness.model.Order;
import com.fitness.model.PrivateTeacher;
import com.fitness.service.ManagementService;
import com.fitness.util.FileUtil;
import com.fitness.util.JsonUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

@Service
public class ManagementServiceImpl implements ManagementService{

	@Resource
	ManagementMapper managementMapper;

	@Override
	public String login(AdminUser aUser) {
		JsonUtil jsonUtil = new JsonUtil();
		AdminUser adminUser = managementMapper.login(aUser);
		if (adminUser==null) {
			jsonUtil.setMsg("登陆失败，账号或密码错误");
			jsonUtil.setSuccess(false);
			return jsonUtil.getJson();
		}else 
			
		jsonUtil.add(adminUser);
		jsonUtil.setMsg("登陆成功");
		return jsonUtil.getJson();
	}

	@Override
	public String addArea(Address address) {
		if (address.getName()==null||address.getName().equals("")) {
			return JsonUtil.blank(false, 400, "请添加数据");
		}
		managementMapper.addArea(address);
		return JsonUtil.blank(true, 200, "添加地区成功");
	}

	

	@Override
	public String adminAdd(AdminUser aUser) {
		managementMapper.adminAdd(aUser);
		return JsonUtil.blank(true, 200, "添加用户成功");
	}

	@Override
	public String adminDelete(AdminUser aUser) {
		managementMapper.adminDelete(aUser);
		return JsonUtil.blank(true, 200, "删除用户成功");
	}

	@Override
	public String adminSelect(AdminUser aUser) {
		JsonUtil js = new JsonUtil();
		List<AdminUser> list = new ArrayList<AdminUser>();
		list  =  managementMapper.adminSelect(aUser);
		if(list!=null && !list.isEmpty()){
			//不为空的情况
			js.setList(list);
		}
		return js.getJson();
	}

	@Override
	public String orderList(Order order) {
			JsonUtil js = new JsonUtil();
			List<Order> list = new ArrayList<Order>();
			list = managementMapper.orderList(order);
			if(list!=null && !list.isEmpty()){
				//不为空的情况
				js.setList(list);
			}
			return js.getJson();
	}

	@Override
	public String orderDelete(Order order) {
		managementMapper.orderDelete(order);
		return JsonUtil.blank(true, 200, "删除订单成功");
	}

	@Override
	public String areaDelete(Address address) {
		managementMapper.areaDelete(address);
		return JsonUtil.blank(true, 200, "删除成功");
	}

	@Override
	public String fitnessDelete(Address address) {
		managementMapper.fitnessDelete(address);
		return JsonUtil.blank(true, 200, "删除成功");
	}

	@Override
	public String classDelete(Class c) {
		managementMapper.classDelete(c);
		return JsonUtil.blank(true, 200, "删除成功");
	}


	@Override
	public String privateDetail(PrivateTeacher p) {
		JsonUtil jsonUtil = new JsonUtil();
		PrivateTeacher privateTeacher = new PrivateTeacher();
		privateTeacher = managementMapper.privateSearch(p);
		jsonUtil.add(privateTeacher);
		return jsonUtil.getJson();
	}

	@Override
	public String privateDelete(PrivateTeacher p) {
		managementMapper.privateDelete(p);
		return JsonUtil.blank(true, 200, "删除成功");
	}

	@Override
	public String campDetail(Camp camp) {
		JsonUtil jsonUtil = new JsonUtil();
		Camp c  =  new Camp();
		c = managementMapper.campDetail(camp);
		jsonUtil.add(c);
		return jsonUtil.getJson();
	}

	@Override
	public String campDelete(Camp camp) {
		managementMapper.campDelete(camp);
		return JsonUtil.blank(true, 200, "删除成功");
	}

	@Override
	@Transactional
	public String addAddress(Address address, HttpServletRequest request) {
		Map<String , String> map;
		//实际的存储目录
		//String dest = "D:/images";//这个最后会拿到配置文件中存储
		//储存文件 并设置路径
		//map = FileUtil.savePicture(address.getFile(),dest, request);
		//拼接url ip应该获取
	//	address.setPictureUrl("/images/"+map.get("url"));//前面的ip应该直接算出来
		//基本信息存储 
		managementMapper.addAddress(address);
		return JsonUtil.blank(true,200,"调用成功");
	}

	@Override
	public String addClass(Class c, HttpServletRequest request) {
		Map<String , String> map;
		//实际的存储目录
		//String dest = "D:/images";//这个最后会拿到配置文件中存储
		//储存文件 并设置路径
		//map = FileUtil.savePicture(c.getFile(),dest, request);
		//拼接url ip应该获取
		//c.setHeadPortrait("/images/"+map.get("url"));//前面的ip应该直接算出来
		//基本信息存储 
		managementMapper.addClass(c);
		return JsonUtil.blank(true,200,"调用成功");
	}

	@Override
	public String addCamp(Camp c, HttpServletRequest request) {
		Map<String , String> map;
		//实际的存储目录
		//String dest = "D:/images";//这个最后会拿到配置文件中存储
		//储存文件 并设置路径
		//map = FileUtil.savePicture(c.getFile(),dest, request);
		//拼接url ip应该获取
		//c.setHeadPortrait("/images/"+map.get("url"));//前面的ip应该直接算出来
		//基本信息存储 
		managementMapper.addCamp(c);
		return JsonUtil.blank(true,200,"调用成功");
	}

	@Override
	public String orderAllDetail(Order order,HttpServletResponse response) {
		System.out.println("------------"+order.toString());
	response.setHeader("Access-Control-Allow-Origin", "*");
	 List<Order> list = new ArrayList<Order>();
	 list = managementMapper.orderAllDetail(order);
	 Integer page = order.getPage();
	 Integer pageSize = order.getPageSize();
	 Page<Order> p  = PageMethod.startPage(page, pageSize);
	 JsonUtil jsonUtil = new JsonUtil();
	 if (list!=null) {
		jsonUtil.setList(list);
	}
	 jsonUtil.setPageInfo(page,p.getTotal(),p.getPages());
	 return jsonUtil.getJson();
	}

	@Override
	public String privateAdd(PrivateTeacher p) {
		managementMapper.privateAdd(p);
		return JsonUtil.blank(true, 200, "添加数据成功");
	}

	@Override
	public String pictureUploading(MultipartFile file, String address, HttpServletRequest res) {
		System.out.println("file======"+file);
		System.out.println("address========"+address);
		//储存文件 并设置路径
		try {
			FileUtil.SaveFileFromInputStream(file.getInputStream(), address, file.getOriginalFilename());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//拼接url ip应该获取
		//c.setHeadPortrait("/images/"+map.get("url"));//前面的ip应该直接算出来
		//基本信息存储 
		return JsonUtil.blank(true, 200, "上传成功");
	}

	@Override
	public String privateOrderDetail(Order order) {
		Order o = new Order();
		o = managementMapper.privateOrderDetail(order);
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add(o);
		return jsonUtil.getJson();
	}

	@Override
	public String coachList(Class c) {
		JsonUtil jsonUtil = new JsonUtil();
		List<Class> arrayList = new ArrayList<Class>();
		arrayList = managementMapper.coachList();
		jsonUtil.setList(arrayList);
		return jsonUtil.getJson();
	}

	@Override
	public String coachDetail(Class c) {
		Class class1= managementMapper.coachDetail(c);
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add(class1);
		return jsonUtil.getJson();
	}

	@Override
	public String coachAdd(Class c) {
		if (c==null) {
		return JsonUtil.blank(false, 400, "数据不能为空");
		}
		managementMapper.coachAdd(c);
		return JsonUtil.blank(true, 200, "添加教练成功");
	}

	@Override
	public String coachDelete(Class c) {
		managementMapper.coachDelete(c);
		return JsonUtil.blank(true, 200, "删除成功");
	}

	@Override
	public String coachupdate(Class c) {
		if (c==null) {
			return JsonUtil.blank(false, 400, "数据不能为空");
			}
			managementMapper.coachupdate(c);
			return JsonUtil.blank(true, 200, "修改教练成功");
	}

	@Override
	public String updateClass(Class c) {
		managementMapper.updateClass(c);
		return JsonUtil.blank(true, 200, "修改成功");
	}

	@Override
	public String updatePrivate(PrivateTeacher p) {
		managementMapper.updatePrivate(p);
		return JsonUtil.blank(true, 200, "修改成功");
	}

	@Override
	public String updateCamp(Camp c) {
		managementMapper.updateCamp(c);
		return JsonUtil.blank(true, 200, "修改成功");
	}

	
}
